//
//  CLEmoiconTools.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/19.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit

//一页显示20条表情数据
let numOfPage = 20
class CLEmoiconTools: NSObject {

    static let sharedTools = CLEmoiconTools()
    //构造函数私有化
    private override init() {
        super.init()
    }
    //创建bundle对象
    private lazy var emoiconBundle:NSBundle = {
    
        //获取路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        //创建bundle对象
        let bundle = NSBundle(path: path)!
        return bundle
    }()
    //读取默认表情数据
    private lazy var defaultEmoiconArray:[CLEmoiconModel] = {
    
        return self .loadEmoiconPlistWithPath("default/info.plist")
    }()
    private lazy var emojiEmoiconArray:[CLEmoiconModel] = {
    
        return self.loadEmoiconPlistWithPath("emoji/info.plist")
    }()
    private lazy var lxhEmoiconArray:[CLEmoiconModel] = {
        
        return self.loadEmoiconPlistWithPath("lxh/info.plist")
    }()
    //准备表情视图需要的数据
    lazy var allEmoiconArray:[[[CLEmoiconModel]]] = {
    
        return [
        
        self.pagesWithEmoicons(self.defaultEmoiconArray),
        self.pagesWithEmoicons(self.emojiEmoiconArray),
        self.pagesWithEmoicons(self.lxhEmoiconArray)
        
        
        ]
    }()
    
    //读取表情plist文件方法
    private func loadEmoiconPlistWithPath(subPath: String) -> [CLEmoiconModel] {
        
        //获取bundle对象
        let bundle:NSBundle = self.emoiconBundle
        //获取info.plist文件路径
        let path = bundle.pathForResource(subPath, ofType: nil)!
        //获取plist文件里面内容
        let emoiconDicArray = NSArray(contentsOfFile: path)!
        //定义可变的数组
        var tempArray = [CLEmoiconModel]()
        for emoiconDic in emoiconDicArray {
            let emoiconModel = CLEmoiconModel(dic: emoiconDic as! [String : AnyObject])
            //如果图片需要处理路径
            if emoiconModel.type == "0" {
                let lastPath = (path as NSString).stringByDeletingLastPathComponent
                emoiconModel.path = lastPath + "/" + emoiconModel.png!
            }
            //添加到临时数组
            tempArray.append(emoiconModel)
        }
        return tempArray
        
    }
    //吧表情数据 拆分为一个二维数组
    private func pagesWithEmoicons(emoiconModel:[CLEmoiconModel]) ->[[CLEmoiconModel]]{
        //根据表情的个数 来计算页数
        let pages = (emoiconModel.count - 1)/numOfPage + 1
        var tempArray = [[CLEmoiconModel]]()
        
        for i in 0..<pages {
            //截取的索引
            let loc = i * numOfPage
            //截取的个数
            var len = numOfPage
            //如果截取长度不够 则取到剩余个数 
            if len + loc > emoiconModel.count {
                len = emoiconModel.count - loc
            }
            let subArray = (emoiconModel as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [CLEmoiconModel]
            
            tempArray.append(subArray)
        }
        
        return tempArray
        
    }
   
    //根据表情字符串查找表情模型
    func selectEmoiconWithChs(chs:String) -> CLEmoiconModel?{
      // $0表示的是这个闭包中的第几个参数   如果闭包只要一行代理  还可以省略 return
//        let defaultEmoicon = defaultEmoiconArray.filter({
////            return $0.chs！== chs
//        })
        
        if let defaultEmoicon = defaultEmoiconArray.filter({$0.chs! == chs }).first {
            return defaultEmoicon
        }
        if let lxhEmoicon = lxhEmoiconArray.filter({$0.chs! == chs}).first {
            return lxhEmoicon
        }
        
//        let defaultEmoicon = defaultEmoiconArray.filter { (emoicon) -> Bool in
//            return emoicon.chs! == chs
//        }.first
//        
//        let lxhEmoicon = lxhEmoiconArray.filter { (emoicon) -> Bool in
//            return emoicon.chs! == chs
//        }.first
//        
//        if defaultEmoicon != nil {
//            return defaultEmoicon
//        }
//        if lxhEmoicon != nil {
//            return lxhEmoicon
//        }
        
        
//        for value in defaultEmoiconArray {
//            if value.chs == chs {
//                return value
//            }
//        }
//        for value in lxhEmoiconArray {
//            if value.chs == chs {
//                return value
//            }
//        }
        return nil
//
    }  
    
}
